package com.yy.job.core.config.xml;

import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import org.springframework.util.CollectionUtils;

import com.google.common.collect.Maps;
import com.yy.job.core.run.worker.ModuleWorker;

/**
 * 模块组
 * 
 * @author eleven.li
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class ModuleXmlConfig implements IXmlConfig {

	@XmlAttribute
	private String moduleCode;

	@XmlAttribute
	private String moduleName;

	@XmlAttribute
	private String jobWorkerSize;

	/**
	 * 任务
	 */
	@XmlElement(name = "jobConfig")
	private List<JobXmlConfig> jobConfigs;

	private Map<String, JobXmlConfig> jobConfigMap;
	
	/**
	 * 每个module对应一个ModuleBoss
	 */
	private ModuleWorker moduleWorker;
	
	@Override
	public void build() {
		if(jobConfigMap == null) {
			jobConfigMap = Maps.newHashMap();
		}
		if (CollectionUtils.isEmpty(this.jobConfigs)) {
			return;
		}
		for (JobXmlConfig jobConfig : getJobConfigs()) {
			jobConfig.build();
			jobConfigMap.put(jobConfig.getJobCode(), jobConfig);
		}
	}

	public String getModuleCode() {
		return this.moduleCode;
	}

	public void setModuleCode(String moduleCode) {
		this.moduleCode = moduleCode;
	}

	public String getModuleName() {
		return this.moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getJobWorkerSize() {
		return jobWorkerSize;
	}

	public void setJobWorkerSize(String jobWorkerSize) {
		this.jobWorkerSize = jobWorkerSize;
	}

	public List<JobXmlConfig> getJobConfigs() {
		return this.jobConfigs;
	}

	public void setJobConfigs(List<JobXmlConfig> jobConfigs) {
		this.jobConfigs = jobConfigs;
	}

	public Map<String, JobXmlConfig> getJobConfigMap() {
		return this.jobConfigMap;
	}

	public void setJobConfigMap(Map<String, JobXmlConfig> jobConfigMap) {
		this.jobConfigMap = jobConfigMap;
	}

	public ModuleWorker getModuleWorker() {
		return moduleWorker;
	}

	public void setModuleWorker(ModuleWorker moduleWorker) {
		this.moduleWorker = moduleWorker;
	}

	public JobXmlConfig findByJobCode(String jobCode) {
		return this.jobConfigMap.get(jobCode);
	}
	
}
